const mongoose = require("mongoose");

// 修正连接字符串，指定数据库名称，去掉 appName 参数
const uri = "mongodb+srv://2892422019:e3aOprR6LKdSvkay@cluster0.ivz1vgz.mongodb.net/test?retryWrites=true&w=majority";

const connectOptions = {
  serverSelectionTimeoutMS: 5000,
  socketTimeoutMS: 45000,
  connectTimeoutMS: 10000,
  retryWrites: true,
  retryReads: true
};

const connectWithRetry = () => {
  mongoose.connect(uri, connectOptions)
    .then(() => {
      console.log("数据库连接成功！");
    })
    .catch((err) => {
      console.log("连接失败，5秒后重试：", err);
      setTimeout(connectWithRetry, 5000);
    });
};

connectWithRetry();

module.exports = mongoose;